import React, { useEffect, useState } from "react";
import { Form, Input, Radio } from "antd";

const FormItem = (props) => {
  /** 定义状态数据 */
  const [form, setForm] = useState(null);
  const [fieldData, setFieldData] = useState({});
  const [type, setType] = useState("basic");
  const [itemKey, setItemKey] = useState();
  const [layout,setLayout] = useState(null)

  /** 初始化状态 */
  useEffect(() => {
    if (props && props.form) {
      setForm(props.form);
    }
    if (props && props.fieldData) {
      setFieldData(props.fieldData);
    }
    if (props && props.type) {
      setType(props.type);
      setLayout(props.type == "basic" ? {
        labelCol: { span: 4 },
        wrapperCol: { span: 19 },
      }:{
        labelCol: { span: 3 },
      })
    }
    setItemKey(props.itemKey);
    
  }, []);

  const bQLineEdit = fieldData.widget == "QLineEdit";
  const bRequired = type == "basic" ? true : false;
  /**条件渲染,根据JSON中的input对象,生成对应的JSX.Element */
  const formItemJSX = bQLineEdit ? (
    <Form.Item
      {...layout}
      colon={bRequired}
      label={fieldData.name}
      name={itemKey}
      initialValue={fieldData?.defaultVal}
      rules={[
        { required: bRequired, message: `${fieldData.name}是必填项` },
        {
          validator: async (rule, values, callback) => {
            if (fieldData.rule && fieldData.rule.lowVal && fieldData.rule.highVal) {
              if (
                parseInt(values) < parseInt(fieldData.rule.lowVal) ||
                parseInt(values) > parseInt(fieldData.rule.highVal)
              ) {             
                return Promise.reject(fieldData.name +
                  "范围:" +
                  fieldData.rule.lowVal +
                  "~" +
                  fieldData.rule.highVal)
              }
            }
            if (
              fieldData.outputType == "float" ||
              fieldData.outputType == "double"
            ) {
              let reg1 = new RegExp(
                "^(([^0][0-9]+|0).([0-9]+)$)|^(([^0][0-9]+|0)$)|^(([1-9]+).([0-9]+)$)|^(([1-9]+)$)"
              );
              if (
                !isNaN(parseFloat(values)) &&
                isFinite(values) &&
                reg1.test(values) &&
                values[-1] != "."
              ) {
                if (
                  values.length != 1 &&
                  values.indexOf(".") == -1 &&
                  values[0] == "0"
                ) {
                 
                  return Promise.reject(fieldData.name + "为数字类型");
                }
              } else {
                return Promise.reject(fieldData.name + "为数字类型");
              }
            } else if (fieldData.outputType == "int") {
              let reg = new RegExp("^(0|[1-9][0-9]*)$");
              if (
                !isNaN(parseInt(values)) &&
                isFinite(values) &&
                reg.test(values)
              ) {
              } else {
                return Promise.reject(fieldData.name + "为整数类型");
              }
            }
            return Promise.resolve();
          },
        },
      ]}
    >
      <Input placeholder={fieldData.about} />
    </Form.Item>
  ) : (
    <Form.Item
      {...layout}
      colon={bRequired}
      label={fieldData.name}
      name={itemKey}
      initialValue={fieldData?.defaultVal}
    >
      <Radio.Group>
        {fieldData &&
          fieldData.maps &&
          fieldData.maps.map((option) => {
            return (
              <Radio key={option.value} value={option.value}>
                {option.name}
              </Radio>
            );
          })}
      </Radio.Group>
    </Form.Item>
  );
  return formItemJSX;
};

export default FormItem;
